@model ReturnRequestSearchModel

@{
    //page title
    ViewBag.PageTitle = T("Admin.ReturnRequests").Text;
    //active menu item (system name)
    Html.SetActiveMenuItemSystemName("Return requests");
}

@{
    const string hideSearchBlockAttributeName = "ReturnRequestsPage.HideSearchBlock";
    var hideSearchBlock = genericAttributeService.GetAttribute<bool>(workContext.CurrentCustomer, hideSearchBlockAttributeName);
}

<div class="content-header clearfix">
    <h1 class="pull-left">
        @T("Admin.ReturnRequests")
    </h1>
    <div class="pull-right">
        &nbsp;
        @await Component.InvokeAsync("AdminWidget", new { widgetZone = AdminWidgetZones.ReturnRequestListButtons })
    </div>
</div>
<div class="content">
    <div class="form-horizontal">
        <div class="panel-group">
            <div class="panel panel-default panel-search">
                <div class="panel-body">
                    <div class="row search-row @(!hideSearchBlock ? "opened" : "")" data-hideAttribute="@hideSearchBlockAttributeName">
                        <div class="search-text">@T("Admin.Common.Search")</div>
                        <div class="icon-search"><i class="fa fa-search" aria-hidden="true"></i></div>
                        <div class="icon-collapse"><i class="fa fa-angle-down" aria-hidden="true"></i></div>
                    </div>

                    <div class="search-body @(hideSearchBlock ? "closed" : "")">

                        <div class="row">
                            <div class="col-md-5">
                                <div class="form-group">
                                    <div class="col-md-4">
                                        <nop-label asp-for="StartDate" />
                                    </div>
                                    <div class="col-md-8">
                                        <nop-editor asp-for="StartDate" />
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-md-4">
                                        <nop-label asp-for="EndDate" />
                                    </div>
                                    <div class="col-md-8">
                                        <nop-editor asp-for="EndDate" />
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-7">
                                <div class="form-group">
                                    <div class="col-md-4">
                                        <nop-label asp-for="CustomNumber" />
                                    </div>
                                    <div class="col-md-8">
                                        <nop-editor asp-for="CustomNumber" />
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-md-4">
                                        <nop-label asp-for="ReturnRequestStatusId" />
                                    </div>
                                    <div class="col-md-8">
                                        <nop-select asp-for="ReturnRequestStatusId" asp-items="Model.ReturnRequestStatusList" />
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-7 col-md-offset-5">
                                <button type="button" id="search-returnrequests" class="btn btn-primary btn-search">
                                    <i class="fa fa-search"></i>
                                    @T("Admin.Common.Search")
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="panel panel-default">
                <div class="panel-body">
                    <p>
                        @T("Admin.ReturnRequests.Description")
                    </p>
                    @await Html.PartialAsync("Table", new DataTablesModel
                    {
                        Name = "returnrequests-grid",
                        UrlRead = new DataUrl("List", "ReturnRequest", null),
                        SearchButtonId = "search-returnrequests",
                        Length = Model.PageSize,
                        LengthMenu = Model.AvailablePageSizes,
                        Filters = new List<FilterParameter>
                        {
                            new FilterParameter(nameof(Model.StartDate), typeof(DateTime?)),
                            new FilterParameter(nameof(Model.EndDate), typeof(DateTime?)),
                            new FilterParameter(nameof(Model.CustomNumber)),
                            new FilterParameter(nameof(Model.ReturnRequestStatusId))
                        },
                        ColumnCollection = new List<ColumnProperty>
                        {
                            new ColumnProperty(nameof(ReturnRequestModel.CustomNumber))
                            {
                                Title = T("Admin.ReturnRequests.Fields.CustomNumber").Text,
                                Width = "100"
                            },
                            new ColumnProperty(nameof(ReturnRequestModel.ProductName))
                            {
                                Title = T("Admin.ReturnRequests.Fields.Product").Text,
                                Width = "350",
                                Render = new RenderCustom("renderColumnProductName")
                            },
                            new ColumnProperty(nameof(ReturnRequestModel.Quantity))
                            {
                                Title = T("Admin.ReturnRequests.Fields.Quantity").Text,
                                Width = "100"
                            },
                            new ColumnProperty(nameof(ReturnRequestModel.CustomerId))
                            {
                                Title = T("Admin.ReturnRequests.Fields.Customer").Text,
                                Width = "350",
                                Render = new RenderCustom("renderColumnCustomerInfo")
                            },
                            new ColumnProperty(nameof(ReturnRequestModel.CustomOrderNumber))
                            {
                                Title = T("Admin.ReturnRequests.Fields.CustomOrderNumber").Text,
                                ClassName =  NopColumnClassDefaults.Button,
                                Width = "200",
                                Render = new RenderCustom("renderColumnOrderInfo")
                            },
                            new ColumnProperty(nameof(ReturnRequestModel.ReturnRequestStatusStr))
                            {
                                Title = T("Admin.ReturnRequests.Fields.Status").Text,
                                Width = "200"
                            },
                            new ColumnProperty(nameof(ReturnRequestModel.CreatedOn))
                            {
                                Title = T("Admin.ReturnRequests.Fields.CreatedOn").Text,
                                Width = "100",
                                Render = new RenderDate()
                            },
                            new ColumnProperty(nameof(ReturnRequestModel.Id))
                            {
                                Title = T("Admin.Common.Edit").Text,
                                Width = "100",
                                ClassName =  NopColumnClassDefaults.Button,
                                Render = new RenderButtonEdit(new DataUrl("Edit"))
                            }
                        }
                    })
                    
                    <script>
                        function renderColumnProductName(data, type, row, meta) {
                            if (row.ProductId > 0) {
                                var textRenderer = $.fn.dataTable.render.text().display;
                                var productInfo = '<a href="@Url.Content("~/Admin/Product/Edit/")' + row.ProductId + '">' + textRenderer(row.ProductName) + '</a>';
                                var attributeInfo = '';
                                if (row.AttributeInfo && row.AttributeInfo.length > 0) {
                                    attributeInfo = '<div>' + row.AttributeInfo +'</div>';
                                }
                                return productInfo + attributeInfo;
                            } else {
                                return 'order item is deleted';
                            }
                        }

                        function renderColumnCustomerInfo(data, type, row, meta) {
                            var textRenderer = $.fn.dataTable.render.text().display;
                            return '<a href="@Url.Content("~/Admin/Customer/Edit/")' + row.CustomerId + '">' + textRenderer(row.CustomerInfo) + '</a>';
                        }

                        function renderColumnOrderInfo(data, type, row, meta) {
                            if (row.OrderId > 0) {
                                var textRenderer = $.fn.dataTable.render.text().display;
                                return '<a class="btn btn-default" href="@Url.Content("~/Admin/Order/Edit/")' + row.OrderId + '"><i class="fa fa-eye"></i>' + textRenderer(row.CustomOrderNumber) + ' - @T("Admin.Common.View")</a>';
                            } else {
                                return 'order item is deleted';
                            }
                        }
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>